/**
 * JAVACC DEMO 1.0
 * @Package com.apache.uct.service.plugins
 */
package com.apache.dict.service.plugins;

import com.apache.dict.vo.ItemListVo;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.dict.entity.DataCate;
import com.apache.dict.entity.DataItem;
import com.apache.dict.manager.DataCateManager;
import com.apache.dict.manager.DataItemManager;
import com.apache.tools.ClassToolsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class DictDelPluginImpl implements PluginConnector {

	private Logger log = LoggerFactory.getLogger(DictDelPluginImpl.class);

	private DataItemManager dataItemManager;

	private DataCateManager dataCateManager;

	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();
		result.setResult("true");
		try {
			String doCode = String.valueOf(vo.getParams("doCode"));
			if (Validator.isNotNull(doCode)) {

				if ("dictCateDel".equals(doCode)) {
					delDictCate(result, vo);
				}
				if ("dictItemDel".equals(doCode)) {
					delDictItem(result, vo);
				}

			}

		} catch (Exception e) {
			result.setResult("false");
			result.setMessage("执行失败！");
		}
		return result;
	}

	private void delDictCate(ResultEntity result, ParamsVo vo) {

		DataCate dataCate = new DataCate();
		dataCate = (DataCate) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), dataCate);
		if (dataCate != null) {
			ParamsVo<DataCate> dataCateVo = new ParamsVo<DataCate>();
			if (Validator.isNotNull(dataCate.getCateId())) {
				dataCateVo.setInfoId(dataCate.getCateId());


				Object obj = dataCateManager.getInfoById(dataCateVo);
				if(obj!=null){
					DataCate dataCateInfo = (DataCate)obj;
					String cateEname = dataCateInfo.getCateEname();
					log.info("删除科目标识["+cateEname+"]");
					ItemListVo itemvo = new ItemListVo();
					ParamsVo voItemsParams = new ParamsVo();
					itemvo.setCateEname(cateEname);
					itemvo.setFatherId("0");
					voItemsParams.setObj(itemvo);
					List<DataItem> list = dataItemManager.getList(voItemsParams);
					if (list!=null && list.size()>0){
						log.info("存在["+list.size()+"]条子项数据，暂时不能直接删除！");
						result.setResult("false");
						result.setMessage("存在子项数据，暂时不能直接删除！");
					}else{
						boolean flag = dataCateManager.deleteInfo(dataCateVo);
						log.info("科目标识["+cateEname+"]删除完成："+flag);
						if (!flag) {
							result.setResult("false");
							result.setMessage("删除失败");
						} else {
							result.setResult("true");
							result.setMessage("删除成功");
							result.setEntity(dataCate.getCateId());
						}
					}
				}

			} else {
				result.setMessage("唯一标识未传递或传递的唯一标识不存在");

			}
		}
	}

	private void delDictItem(ResultEntity result, ParamsVo vo) {

		DataItem dataItem = new DataItem();
		dataItem = (DataItem) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), dataItem);
		if (dataItem != null) {
			ParamsVo<DataItem> dataCateVo = new ParamsVo<DataItem>();
			if (Validator.isNotNull(dataItem.getItemId())) {
				dataCateVo.setInfoId(dataItem.getItemId());
				boolean flag = dataItemManager.deleteInfo(dataCateVo);
				if (!flag) {
					result.setResult("false");
					result.setMessage("删除失败");
				} else {
					result.setResult("true");
					result.setMessage("删除成功");
					result.setEntity(dataItem.getItemId());
				}
			} else {
				result.setMessage("唯一标识未传递或传递的唯一标识不存在");

			}
		}
	}

	public void setDataItemManager(DataItemManager dataItemManager) {
		this.dataItemManager = dataItemManager;
	}

	public void setDataCateManager(DataCateManager dataCateManager) {
		this.dataCateManager = dataCateManager;
	}

}
